import {onMounted,ref} from "vue";
import {deleteRoleById, getRoleList} from "@/module-base/api/sysManage.js";
import {Message, Modal} from "view-ui-plus";

export function useRoleHandle(){
    const roleData = ref([])
    async function handleGetList(pageIndex, pageSize) {
        const rs = await getRoleList({
            pageIndex: pageIndex || 1,
            pageSize: pageSize || 20
        })
        if (rs.success) {
            roleData.value = rs.data
        }
    }
    onMounted(() => {
        handleGetList()
    })

    async function deleteRole(id) {
        console.log(id)
        if(!id){
            Message.error('请选择要删除的数据')
            return
        }
        Modal.confirm({
            title: '删除角色',
            content: '<p>请确认是否删除选中的角色</p>',
            loading: true,
            onOk: async () => {
                const rs=await deleteRoleById(id)
                if(rs.success){
                    handleGetList()
                    Message.success('删除成功')
                    Modal.remove()
                }
            }
        });

    }
    return{
        roleData,
        deleteRole,
        handleGetList
    }
}